﻿namespace Baggr.ClientSite.Models
{
    using System.Collections.Generic;
    using System.IO;
    using System.ComponentModel;

    using System;
    [TypeConverter(typeof(FeedItemsSortingConverter)), Serializable]
    public class FeedItemsSorting
        : IEquatable<FeedItemsSorting>
    {
        private enum Values
        {
            PublishDate = 0,
            Rating = 1,
            Relevance = 2
        }

        [NonSerialized]
        private static readonly FeedItemsSorting _PublishDate = new FeedItemsSorting(Values.PublishDate);

        [NonSerialized]
        private static readonly FeedItemsSorting _Relevance = new FeedItemsSorting(Values.Relevance);

        [NonSerialized]
        private static readonly FeedItemsSorting _Rating = new FeedItemsSorting(Values.Rating);


        private readonly Values _Value;

        private FeedItemsSorting(Values value)
        {
            if (!Enum.IsDefined(typeof(Values), value))
                throw new ArgumentException("Value doesn't exist", "value");
            _Value = value;
        }

        public static FeedItemsSorting PublishDate
        {
            get
            {
                return _PublishDate;
            }
        }
        public static FeedItemsSorting Relevance
        {
            get
            {
                return _Relevance;
            }
        }
        public static FeedItemsSorting Rating
        {
            get
            {
                return _Rating;
            }
        }

        public string Text
        {
            get
            {
                if (IsByPublishDate)
                    return "Sort as published";
                if (IsByRating)
                    return "Sort by rating";
                if (IsByRelevance)
                    return "Sort by relevance";
                throw new InvalidDataException("Unknown type");
            }
        }

        public string AsString
        {
            get
            {
                return ToString();
            }
        }

        public static FeedItemsSorting Parse(string value)
        {
            switch(value.ToUpper())
            {
                case "PUBLISHDATE":
                    return PublishDate;
                case "RATING":
                    return Rating;
                case "RELEVANCE":
                    return Relevance;
                default:
                    return null;
            }
        }

        public bool IsByPublishDate
        {
            get
            {
                return _Value == Values.PublishDate;
            }
        }

        public bool IsByRating
        {
            get
            {
                return _Value == Values.Rating;
            }
        }

        public bool IsByRelevance
        {
            get
            {
                return _Value == Values.Relevance;
            }
        }

        public bool Equals(FeedItemsSorting other)
        {
            if (ReferenceEquals(null, other))
                return false;
            if (ReferenceEquals(this, other))
                return true;
            return _Value == other._Value;
        }

        public override string ToString()
        {
            return Enum.GetName(typeof (Values), _Value);
        }

        public override int GetHashCode()
        {
            return _Value.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj as FeedItemsFilter);
        }

        public static IEnumerable<FeedItemsSorting> All
        {
            get
            {
                yield return PublishDate;
                yield return Relevance;
                yield return Rating;
            }
        }
    }
}
